package com.jule.teenpatti.game.api.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jule.teenpatti.game.api.BaseServlet;
import com.jule.teenpatti.game.api.entity.CommonConfigEntity;
import com.jule.teenpatti.game.api.entity.RoomConfigEntity;
import com.jule.teenpatti.game.dao.DBUtil;
import com.jule.teenpatti.game.dao.bean.CommonConfigModel;
import com.jule.teenpatti.game.dao.bean.RoomConfigModel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Path(value = "/api/data")
public class DataServlet extends BaseServlet {

    @GET
    @Path("/selectAllTeenPattiRoom")
    @Produces({MediaType.APPLICATION_JSON})
    public String selectAllRoomConfig() {
        CodeObject codeObject = new CodeObject();
        try {
            List<RoomConfigModel> list = DBUtil.selectAllRoom();
            List<RoomConfigEntity> array = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                array.add(new RoomConfigEntity(list.get(i)));
            }
            codeObject.setCode(0);
            codeObject.setMsg("");
            codeObject.setResult(JSONArray.toJSON(array).toString());
            Object obj = JSONObject.toJSON(codeObject);
            return obj.toString();
        } catch (Exception e) {
            codeObject.setCode(5000);
            codeObject.setMsg("服务器内部错误");
            codeObject.setResult("");
            Object obj = JSONObject.toJSON(codeObject);
            logger.error("查询房间配置出错：{}", e.getMessage());
            return obj.toString();
        }
    }

    @GET
    @Path("/updateTeenPattiRoom")
    @Produces({MediaType.APPLICATION_JSON})
    public String updateRoomConfig(@QueryParam("id") int id,
                                   @QueryParam("roomId") String roomId,
                                   @QueryParam("minJoinTable") Long minJoinTable,
                                   @QueryParam("ante") Long ante,
                                   @QueryParam("firstBetScore") Long firstBetScore,
                                   @QueryParam("maxBetScore") Long maxBetScore,
                                   @QueryParam("serviceCharge") double serviceCharge,
                                   @QueryParam("maxRound") Integer maxRound,
                                   @QueryParam("tipValue") Integer tipValue,
                                   @QueryParam("doubleRoles") String doubleRoles,
                                   @QueryParam("xiQian") int xiQian) {
        CodeObject codeObject = new CodeObject();
        try {
            RoomConfigModel roomConfigModel = DBUtil.selectRoom(id);
            roomConfigModel.setId(id);
            roomConfigModel.setRoomId(roomId);
            roomConfigModel.setMinScore4JoinTable(minJoinTable);
            roomConfigModel.setAnte(ante);
            roomConfigModel.setFirstBaseBetScore(firstBetScore);
            roomConfigModel.setMaxBetScore(maxBetScore);
            roomConfigModel.setServiceChargeRate(serviceCharge);
            roomConfigModel.setMaxBlindRound(maxRound);
            roomConfigModel.setTipValue(tipValue);
            roomConfigModel.setDoubleRoles("[" + doubleRoles + "]");
            roomConfigModel.setXiQian(xiQian);
            int in = DBUtil.updateRoom(roomConfigModel);
            if (in == 1) {
                RoomConfigEntity roomConfigEntity = new RoomConfigEntity(DBUtil.selectRoom(id));
                codeObject.setCode(0);
                codeObject.setMsg("");
                codeObject.setResult(JSONObject.toJSON(roomConfigEntity).toString());
            } else {
                codeObject.setCode(5001);
                codeObject.setMsg("参数错误");
                codeObject.setResult("");
            }
            Object obj = JSONObject.toJSON(codeObject);
            return obj.toString();
        } catch (Exception e) {
            codeObject.setCode(5000);
            codeObject.setMsg("服务器内部错误");
            codeObject.setResult("");
            Object obj = JSONObject.toJSON(codeObject);
            logger.error("修改房间配置出错：{}", e.getMessage());
            return obj.toString();
        }
    }

    @GET
    @Path("/selectAllTeenPattiCommon")
    @Produces({MediaType.APPLICATION_JSON})
    public String selectAllCommonConfig() {
        CodeObject codeObject = new CodeObject();
        try {
            List<CommonConfigModel> list = DBUtil.selectAllCommon();
            codeObject.setCode(0);
            codeObject.setMsg("");
            codeObject.setResult(JSONArray.toJSON(new CommonConfigEntity(list.get(0))).toString());
            Object obj = JSONObject.toJSON(codeObject);
            return obj.toString();
        } catch (Exception e) {
            codeObject.setCode(5000);
            codeObject.setMsg("服务器内部错误");
            codeObject.setResult("");
            Object obj = JSONObject.toJSON(codeObject);
            logger.error("查询通用配置出错：{}", e.getMessage());
            return obj.toString();
        }
    }

    @GET
    @Path("/updateTeenPattiCommon")
    @Produces({MediaType.APPLICATION_JSON})
    public String updateCommonConfig(@QueryParam("id") int id,
                                     @QueryParam("gameStartCD") int gameStartCD,
                                     @QueryParam("betCD") int betCD) {
        CodeObject codeObject = new CodeObject();
        try {
            CommonConfigModel configModel = DBUtil.selectCommon(id);
            configModel.setGameStartCountDownSec(gameStartCD);
            configModel.setBetCountDownSec(betCD);
            int in = DBUtil.updateCommon(configModel);
            if (in == 1) {
                CommonConfigEntity common = new CommonConfigEntity(DBUtil.selectCommon(id));
                codeObject.setCode(0);
                codeObject.setMsg("");
                codeObject.setResult(JSONObject.toJSON(common).toString());
            } else {
                codeObject.setCode(5001);
                codeObject.setMsg("参数错误");
                codeObject.setResult("");
            }
            Object obj = JSONObject.toJSON(codeObject);
            return obj.toString();
        } catch (Exception e) {
            codeObject.setCode(5000);
            codeObject.setMsg("服务器内部错误");
            codeObject.setResult("");
            Object obj = JSONObject.toJSON(codeObject);
            logger.error("修改通用配置出错：{}", e.getMessage());
            return obj.toString();
        }
    }
}


@Setter
@Getter
@ToString
class CodeObject {
    private int code;
    private String msg;
    private String result;
}
